'''
Descripttion: 
Author: White
Date: 2025-01-09 14:27:52
LastEditTime: 2025-01-09 14:28:40
'''
from flask import Flask, render_template_string, jsonify
from app import app

# 主页面模板
index_html = '''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Main Page</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.nav-link').click(function(event) {
                event.preventDefault();
                var page = $(this).attr('data-page');
                $.ajax({
                    url: '/content/' + page,
                    method: 'GET',
                    success: function(data) {
                        if (data.html) {
                            $('#content').html(data.html);
                        } else if (data.error) {
                            alert('Error: ' + data.error);
                        }
                    },
                    error: function(xhr, status, error) {
                        alert('AJAX Error: ' + error);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <div class="nav">
        <a href="#" class="nav-link" data-page="form">Form</a>
        <a href="#" class="nav-link" data-page="table">Table</a>
    </div>
    <div id="content">Loading...</div>
</body>
</html>
'''
 
# 表单内容模板
form_html = '''
<h1>Form</h1>
<form action="/submit" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    <input type="submit" value="Submit">
</form>
'''
 
# 表格内容模板（这里只是静态示例，实际中可能从数据库获取数据）
table_html = '''
<h1>Table</h1>
<table border="1">
    <tr>
        <th>ID</th>
        <th>Name</th>
    </tr>
    <tr>
        <td>1</td>
        <td>John Doe</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Jane Smith</td>
    </tr>
</table>
'''
 
# @app.route('/')
# def index():
#     return render_template_string(index_html)
 
@app.route('/content/<page>')
def content(page):
    if page == 'form':
        return jsonify({'html': form_html})
    elif page == 'table':
        return jsonify({'html': table_html})
    else:
        return jsonify({'error': 'Page not found'})